package com.beiding.businessplatform.starter;

import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DemoApplication {

    public static void main(String[] args) throws Exception {

        URLClassLoader classLoader = (URLClassLoader) DemoApplication.class.getClassLoader();

        URL[] urLs = classLoader.getURLs();

        List<URL> files = new ArrayList<>();

        File zf = new File("C:\\Users\\dingchanglei\\Desktop\\我的平台\\验证\\jpatest\\target");

        String pre = zf.getAbsolutePath();

        URL u = null;

        for (URL urL : urLs) {
            File file = new File(URLDecoder.decode(urL.getFile(), "utf8"));
            String absolutePath = file.getAbsolutePath();
            if (!absolutePath.startsWith(pre)) {
                files.add(urL);
            } else {
                u = urL;
            }
        }

        /*



         */

        URL[] urls = new URL[files.size()];

        files.toArray(urls);

        //TODO 基础类路径
        URLClassLoader baseClassLoader = new URLClassLoader(urls, null);

        Object[] contexts = new Object[2];

        t(contexts, u, baseClassLoader);
        Scanner scanner = new Scanner(System.in);

        while (true) {
            Thread.sleep(1000);
            if (contexts[0] != null) {

                System.out.println("等待关闭上下文重启");
                String line = scanner.nextLine();
                System.out.println("关闭上下文中");
                contexts[0].getClass().getMethod("close").invoke(contexts[0]);
                contexts[0].getClass().getMethod("stop").invoke(contexts[0]);
                contexts[0].getClass().getMethod("stop").invoke(contexts[0]);

                while (true) {
                    Thread.sleep(500);
                    try {
                        boolean isActive = (boolean) contexts[0].getClass().getMethod("isActive").invoke(contexts[0]);

                        if (!isActive) {
                            System.out.println("已成功关闭");
                            break;
                        }

                    } catch (Exception ignore) {
                        break;
                    }
                }

                contexts[0] = null;
                t(contexts, u, baseClassLoader);
            }

        }
    }


    /*

        同一时间只能开启一个线程

     */
    private static void t(Object[] contexts, URL u, URLClassLoader baseClassLoader) throws MalformedURLException {


        URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{new File("C:\\Users\\dingchanglei\\Desktop\\我的平台\\验证\\jpatest\\target\\classes1").toURI().toURL()}, baseClassLoader);

        URLClassLoader fucl = new URLClassLoader(new URL[]{u}, urlClassLoader);

        Thread thread = new Thread(() -> {
            try {
                //TODO 获取类
                Class<?> loadClass = fucl.loadClass("com.example.Application");
                Class<?> sa = fucl.loadClass("org.springframework.boot.SpringApplication");
                Method run = sa.getMethod("run", Class.class, String[].class);
                contexts[0] = run.invoke(null, loadClass, new String[]{"--server.port=" + 8002, "--spring.jmx.enabled=false"});

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        thread.setContextClassLoader(urlClassLoader);
        thread.start();

    }


}
